import Vue from 'vue'
import Router from 'vue-router'
import iView from 'iview'
import { getToken } from '../utils'
Vue.use(iView)

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/account/login')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home')
    },
    {
      path: '/article/list',
      name: 'article-list',
      component: () => import('@/views/article/list.vue')
    },
    {
      path: '/article/:articleId',
      name: 'article',
      component: () => import('@/views/article')
    },
    {
      path: '/edit-article/:articleId?',
      name: 'createArticle',
      component: () => import('@/views/article/create')
    },
    // {
    //   path: '/personal',
    //   name: 'personal',
    //   component: () => import('@/views/personal/personal.vue'),
    //   children: [
    //     {
    //       path: 'editArticle',
    //       name: 'editArticle',
    //       component: () => import('@/views/personal/editArticle/editArticle.vue')
    //     }
    //   ]
    // },
    {
      path: '/editArticle',
      name: 'editArticle',
      component: () => import('@/views/editArticle/editArticle.vue')
    },
    {
      path: '/baby',
      name: 'user-introduce',
      component: () => import('@/views/userIntroduce/intro')
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 如果去登录页， 且已登录， 则直接进首页
  iView.LoadingBar.start()
  if (to.path === '/login' && getToken()) {
    next('/home')
  } else {
    next()
  }
})

router.afterEach((route) => {
  iView.LoadingBar.finish()
})

export default router
